<template>
  <a-card :bordered="false">
    <!-- 查询区域 -->
    <div class="table-page-search-wrapper">
      <a-form layout="inline" @keyup.enter.native="searchQuery">
        <a-row :gutter="24">
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <a-form-item :label="$t('活动类型')">
              <a-select v-model="queryParam.type">
                <a-select-option :value="''">
                  {{ $t('全部') }}
                </a-select-option>
                <a-select-option v-for="(item, index) in activityList" :key="index" :value="item.value">
                  {{ item.title }}
                </a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :xl="5" :lg="6" :md="7" :sm="24">
            <a-form-item :label="$t('状态')">
              <a-select v-model="queryParam.state">
                <a-select-option v-for="(item, index) in stateList" :key="index" :value="item.value">
                  {{ item.title }}
                </a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :xl="5" :lg="6" :md="7" :sm="24">
            <a-form-item :label="$t('活动名称')">
              <a-input :placeholder="$t('请输入活动名称')" v-model="queryParam.name"></a-input>
            </a-form-item>
          </a-col>
          <a-col :xl="6" :lg="7" :md="8" :sm="24" v-if="!tenantId">
            <a-form-item :label="$t('门店')">
              <a-select v-model="queryParam.tenantId">
                <a-select-option v-for="(item, i) in shops" :key="i" :value="item.value">
                  {{ item.title }}<span v-if="item.value != ''">（{{ item.value }}）</span></a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <span style="float: left; overflow: hidden" class="table-page-search-submitButtons">
              <a-button type="primary" @click="searchQuery" icon="search">{{$t('查询')}}</a-button>
              <a-button type="primary" @click="searchReset" icon="reload" style="margin-left: 8px">{{$t('重置')}}</a-button>
              <a @click="handleToggleSearch" style="margin-left: 8px">
                {{ toggleSearchStatus ? $t('收起') : $t('展开') }}
                <a-icon :type="toggleSearchStatus ? 'up' : 'down'" />
              </a>
            </span>
          </a-col>
        </a-row>
        <a-row :gutter="24">
          <template v-if="toggleSearchStatus">
          </template>
        </a-row>
      </a-form>
    </div>
    <!-- 查询区域-END -->

    <!-- 操作按钮区域 -->
    <div class="table-operator">
      <a-button type="primary" icon="plus" v-for="(item,i) in activityList" :key="i" @click="handleAdds(item.value)" v-has="'BisActivityController:add'">
        {{item.title}}
      </a-button>
    </div>

    <!-- table区域-begin -->
    <div>
      <a-table
        ref="table"
        size="middle"
        :scroll="{ x: true }"
        bordered
        rowKey="id"
        :columns="columns"
        :dataSource="dataSource"
        :pagination="ipagination"
        :loading="loading"
        class="j-table-force-nowrap"
        @change="handleTableChange"
      >
        <span slot="action" slot-scope="text,record">
          <a @click="handleDetail(record,false)" v-has="'BisActivityController:edit'" v-if="(tenantId && tenantId == record.tenantId)||(!tenantId && record.tenantId == 0)">{{$t('编辑')}}</a>
          <a-popconfirm :title="$t('删除了该活动将立即失效，您确定删除该活动吗?')" @confirm="() => handleDelete(record.id)">
            <a style="margin-left: 8px" v-has="'BisActivityController:delete'" v-if="(tenantId && tenantId == record.tenantId)||(!tenantId && record.tenantId == 0)">{{$t('删除')}}</a>
          </a-popconfirm>
          <a style="margin-left: 8px" @click="handleDetail(record,true)">{{$t('详情')}}</a>
          <a style="margin-left: 8px" v-if="record.type != 3 && record.type != 4 && record.type != 5" @click="activityRecords(record)">{{$t('活动记录')}}</a>
          <a style="margin-left: 8px" v-if="record.type == 5" @click="activityRecords(record)">{{$t('活动记录')}}</a>
          <a style="margin-left: 8px" v-if="record.type==3" @click="drawList(record)">{{$t('抽奖次数')}}</a>
          <a style="margin-left: 8px" v-if="record.type==3" @click="drawRecord(record)">{{$t('抽奖记录')}}</a>
          <a style="margin-left: 8px" v-if="record.type==3" @click="drawSum(record)">{{$t('抽奖汇总')}}</a>
        </span>
      </a-table>
    </div>

    <add-activity-modal ref="modalForm" @ok="modelClose"></add-activity-modal>
    <activity-records-modal ref="activityForm" ></activity-records-modal>
    <add-activity-detail-modal ref="detailForm" @ok="modelClose"></add-activity-detail-modal>
    <draw-list-modal ref="listForm"></draw-list-modal>
    <draw-record-modal ref="recordForm"></draw-record-modal>
    <draw-sum-modal ref="sumForm"></draw-sum-modal>
    <presale-records-modal ref="presaleForm" ></presale-records-modal>
  </a-card>
</template>

<script>
import '@/assets/less/TableExpand.less'
import Vue from 'vue'
import { mixinDevice } from '@/utils/mixin'
import { JeecgListMixin } from '@/mixins/JeecgListMixin'
import { initDictOptions, filterMultiDictText } from '@/components/dict/JDictSelectUtil'
import AddActivityModal from './modules/AddActivityModal'
import ActivityRecordsModal from './modules/ActivityRecordsModal'
import AddActivityDetailModal from './modules/AddActivityDetailModal'
import DrawListModal from './modules/DrawListModal'
import DrawRecordModal from './modules/DrawRecordModal'
import DrawSumModal from './modules/DrawSumModal'
import PresaleRecordsModal from './modules/PresaleRecordsModal'

export default {
  name: 'GoodsActivity',
  mixins: [JeecgListMixin, mixinDevice],
  components: {
    AddActivityModal,
    ActivityRecordsModal,
    AddActivityDetailModal,
    DrawListModal,
    DrawRecordModal,
    DrawSumModal,
    PresaleRecordsModal
  },
  data() {
    return {
      description: '商品打折管理页面',
      queryParam: {
        column: 'createTime',
        order: 'desc',
        type:'',
        state:'1',
        name:'',
        tenantId: ''
      },
      // 表头
      columns: [
        {
          title: this.$t('活动名称'),
          align: 'center',
          dataIndex: 'name',
        },
        {
          title: this.$t('活动类型'),
          align: 'center',
          dataIndex: 'type_dictText',
        },
        {
          title: this.$t('状态'),
          align: 'center',
          dataIndex: 'state_dictText',
        },
        {
          title: this.$t('有效期'),
          align: 'center',
          dataIndex: 'beginTime',
          customRender:(text,record)=>{
            return record.beginTime + '~' +record.endTime
          }
        },
        {
          title: this.$t('活动说明'),
          align: 'center',
          dataIndex: 'intro',
          customCell: () => {
            return {
              style: {
                maxWidth: '600px',
                wordWrap: 'break-word',
                wordBreak: 'normal',
                whiteSpace: 'pre-wrap',
              },
            }
          },
        },
        {
          title: this.$t('操作列'),
          align: 'center',
          dataIndex: 'action',
          scopedSlots: { customRender: 'action' },
        },
      ],
      url: {
        list: '/activity/bisActivity/list',
        delete: '/activity/bisActivity/delete',
        exportXlsUrl: '/activity/bisActivity/exportXls',
        importExcelUrl: '/activity/bisActivity/importExcel',
      },
      dictOptions: {},
      stateList:[],
      activityList:[],
      deviceList:[],
      tenantId: Vue.ls.get('TENANT_ID'),
      shops: []
    }
  },
  async created() {
    if (!this.tenantId) {
      await initDictOptions('bis_shop,name,code').then((res) => {
        if (res.success) {
          this.shops = res.result
          this.shops.unshift({ value: '', title: this.$t('全部') })
        }
      })
      let dtl = {
        title: this.$t('门店编码'),
        align: 'center',
        dataIndex: 'tenantId',
        width: 100,
        customCell: (r, i) => {
          return {
            style: {
              whiteSpace: 'normal',
            },
          }
        },
      },
        dtl1 = {
          title: this.$t('门店名称'),
          align: 'center',
          dataIndex: 'tenantId_dictText',
          width: 140,
          customRender: (text, record) => {
            if (text) {
              return text
            }
            let x = this.shops.findIndex(s => { return s.value === record.tenantId.toString() })
            if (x > -1) {
              return this.shops[x].title
            }
            return record.tenantId
          },
          customCell: (r, i) => {
            return {
              style: {
                whiteSpace: 'normal',
              },
            }
          },
        }
      this.columns.unshift(dtl1)
      this.columns.unshift(dtl)
    }
  },
  computed: {
    importExcelUrl: function () {
      return `${window._CONFIG['domianURL']}/${this.url.importExcelUrl}`
    },
  },
  methods: {
    initDictConfig() {
      initDictOptions('bis_activity.type').then((res) => {
        if (res.success) {
          this.activityList = res.result
          //this.activityList.unshift({ title: this.$t('全部'), value: '' })
        }
      })
        initDictOptions('bis_activity.state').then((res) => {
          if (res.success) {
            this.stateList = res.result
          }
        })
    },
    searchReset() {
      this.queryParam = {
        column: 'createTime',
        order: 'desc',
        state:'1',
        type:'',
        name:'',
        tenantId: ''
      }
      this.loadData(1)
    },
    modelClose(){
      this.loadData(1)
    },
    activityRecords(record){
      if(record.type == 5){
        this.$refs.presaleForm.show(record)
      }
      if(record.type != 3 && record.type != 4 && record.type != 5){
        this.$refs.activityForm.show(record)
      }
    },
    drawList(record){
      this.$refs.listForm.show(record)
    },
    drawRecord(record){
      this.$refs.recordForm.show(record)
    },
    drawSum(record){
      this.$refs.sumForm.show(record)
    },
    handleDetail(record,state){
      this.$refs.detailForm.edit(record,state)
      if(state){
        this.$refs.detailForm.title = this.$t('查看');
      }else{
        this.$refs.detailForm.title = this.$t('编辑');
      }
      this.$refs.detailForm.disableSubmit = false;
    },
    handleAdds(value) {
      this.$refs.detailForm.add(value)
      this.$refs.detailForm.title = this.$t('新增')
      this.$refs.detailForm.disableSubmit = false
    },
  },
}
</script>
<style scoped>
@import '~@assets/less/common.less';
</style>